#include "modbus.h"
#include "ui_modbus.h"

Modbus::Modbus(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Modbus)
{
    ui->setupUi(this);
    ui->ac_oc->key=101;
    ui->let_cold->key=103;
    ui->let_hot->key=104;
    ui->c_l->key=105;
    ui->ty_y->key=106;
    ui->ac_oc->type=1;
    ui->let_cold->type=1;
    ui->let_hot->type=1;
    ui->c_l->type=1;
    ui->ty_y->type=1;
    ui->ac_oc->val.b_val=0;
    ui->let_cold->val.b_val=0;
    ui->let_hot->val.b_val=0;
    ui->c_l->val.b_val=0;
    ui->ty_y->val.b_val=0;
    ui->let_cold->setDisabled(1);
    ui->let_hot->setDisabled(1);
    ui->ac_temp->setDisabled(1);
    connect(ui->ac_oc,SIGNAL(clicked(bool)),SLOT(send(bool)));
    connect(ui->c_l,SIGNAL(clicked(bool)),SLOT(send(bool)));
    connect(ui->ty_y,SIGNAL(clicked(bool)),SLOT(send(bool)));
    btp=new QButtonGroup(this);
    btp->addButton(ui->let_cold,1);
    btp->addButton(ui->let_hot,2);
    connect(btp,SIGNAL(buttonToggled(int,bool)),this,SLOT(btpsend(int,bool)));
    connect(ui->ac_temp,SIGNAL(valueChanged(QString)),this,SLOT(tempslot(QString)));


}

Modbus::~Modbus()
{
    delete ui;
}


void Modbus::send(bool val)
{
    QMQTT::Message message;
    message.setTopic("/app/data/down");
    QJsonObject json;
    QJsonObject djson;
    if(ui->ac_oc==sender())
    {
        json.insert("type",2);
        djson.insert("key",ui->ac_oc->key);
        QString sval= QString::number(val);
        djson.insert("val",sval);
        ui->ac_oc->val.b_val=val;
        json.insert("data",QJsonValue(djson));
        if(ui->ac_oc->isChecked())
        {
            ui->let_cold->setDisabled(0);
            ui->let_hot->setDisabled(0);
            ui->ac_temp->setDisabled(0);
        }else
        {
            ui->let_cold->setDisabled(1);
            ui->let_hot->setDisabled(1);
            ui->ac_temp->setDisabled(1);
        }
    } else if(ui->c_l==sender())
    {
        json.insert("type",2);
        djson.insert("key",ui->c_l->key);
        QString sval= QString::number(val);
        djson.insert("val",sval);
        ui->c_l->val.b_val=val;
        json.insert("data",QJsonValue(djson));
    } else if(ui->ty_y==sender())
    {
        json.insert("type",2);
        djson.insert("key",ui->ty_y->key);
        QString sval= QString::number(val);
        djson.insert("val",sval);
        ui->ty_y->val.b_val=val;
        json.insert("data",QJsonValue(djson));
    }
    QJsonDocument document;
    document.setObject(json);
    QByteArray byte_array = document.toJson(QJsonDocument::Compact);
    message.setPayload(byte_array);
    //发布
    GlobalVal ::mqtt->publish(message);

}
void Modbus::btpsend(int id, bool val)
{
    QMQTT::Message message;
    message.setTopic("/app/data/down");
    QJsonObject json;
    QJsonObject djson;
    if(id==1)
    {
        json.insert("type",2);
        djson.insert("key",ui->let_cold->key);
        QString sval= QString::number(val);
        djson.insert("val",sval);
        ui->let_cold->val.b_val=val;
        json.insert("data",QJsonValue(djson));
    }else if(id==2)
    {
        json.insert("type",2);
        djson.insert("key",ui->let_hot->key);
        QString sval= QString::number(val);
        djson.insert("val",sval);
        ui->let_hot->val.b_val=val;
        json.insert("data",QJsonValue(djson));
    }
    QJsonDocument document;
    document.setObject(json);
    QByteArray byte_array = document.toJson(QJsonDocument::Compact);
    message.setPayload(byte_array);
    //发布
    GlobalVal ::mqtt->publish(message);
}

void Modbus::tempslot(QString str)
{
    qDebug() <<str;
    QMQTT::Message message;
    message.setTopic("/app/data/down");
    QJsonObject json;
    QJsonObject djson;
    json.insert("type",2);
    djson.insert("key",102);
    djson.insert("val",str);
    json.insert("data",QJsonObject(djson));
    QJsonDocument document;
    document.setObject(json);
    QByteArray byte_array=document.toJson(QJsonDocument::Compact);
    message.setPayload(byte_array);
    GlobalVal::mqtt->publish(message);

}

void Modbus::dataUpSlot(QByteArray arry)
{

    QString sjson(arry);
    qDebug()<< sjson;
    QJsonParseError jsonError;
    QJsonDocument doucment = QJsonDocument::fromJson(sjson.toLatin1(), &jsonError);
    if (!doucment.isNull() && jsonError.error == QJsonParseError::NoError)
    {
        if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
        {
            if (doucment.isObject())
            {
                QJsonObject object = doucment.object();
                if (object.contains("type"))
                {  // 包含指定的 key
                    QJsonValue value = object.value("type");
                    if (value.isDouble())
                    {
                        type=value.toVariant().toInt();
                        qDebug() << "type : " << type;
                    }
                }
                if(type ==1)
                {
                    if (object.contains("result"))
                    {
                        QJsonValue value = object.value("result");
                        if (value.isDouble())
                        {
                            result = value.toVariant().toInt();
                            qDebug() << "result : " << result;
                        }
                    }
                    if(result==0)
                    {
                        if(object.contains("data"))
                        {
                            QJsonValue value = object.value("data");
                            if(value.isArray())
                            {
                                QJsonArray arr=value.toArray();
                                int nsize=arr.size();
                                for(int i=0;i<nsize;i++)
                                {

                                    QJsonValue value=arr.at(i);
                                    if (value.isObject())
                                    {
                                        QJsonObject obj=value.toObject();
                                        if(obj.contains("key"))
                                        {
                                        QJsonValue value = obj.value("key");
                                        qDebug() << value.toInt();
                                        switch (value.toInt()) {
                                        case 101:
                                               if(obj.contains("val"))
                                               {
                                                   QJsonValue value = obj.value("val");
                                                   //QString text=value.toDouble();
                                                   QString text=value.toString();
                                                   int val=text.toInt();
                                                   ui->ac_oc->setChecked(val);
                                                   if(ui->ac_oc->isChecked())
                                                   {
                                                       ui->let_cold->setDisabled(0);
                                                       ui->let_hot->setDisabled(0);
                                                       ui->ac_temp->setDisabled(0);

                                                   }else
                                                   {
                                                       ui->let_cold->setDisabled(1);
                                                       ui->let_hot->setDisabled(1);
                                                       ui->ac_temp->setDisabled(1);
                                                   }
                                               }
                                            break;
                                        case 102:
                                            if(obj.contains("val"))
                                            {
                                                //
                                                QJsonValue value = obj.value("val");
                                                //QString text=value.toDouble();
                                                QString text=value.toString();
                                                //ui->ac_temp->setValue(text.toInt());
                                                ui->ac_temp->setValue(text.toDouble());
                                            }
                                            break;
                                        case 103:
                                            if(obj.contains("val"))
                                            {
                                                QJsonValue value = obj.value("val");
                                                //QString text=value.toDouble();
                                                QString text=value.toString();
                                                int val=text.toInt();
                                                ui->let_cold->setChecked(val);

                                            }
                                            break;
                                        case 104:
                                            if(obj.contains("val"))
                                            {
                                                QJsonValue value = obj.value("val");
                                                //QString text=value.toDouble();
                                                QString text=value.toString();
                                                int val=text.toInt();
                                                ui->let_hot->setChecked(val);

                                            }
                                            break;
                                        case 105:
                                            if(obj.contains("val"))
                                            {
                                                QJsonValue value = obj.value("val");
                                                //QString text=value.toDouble();
                                                QString text=value.toString();
                                                int val=text.toInt();
                                                ui->c_l->setChecked(val);

                                            }
                                            break;
                                        case 106:
                                            if(obj.contains("val"))
                                            {
                                                QJsonValue value = obj.value("val");
                                                //QString text=value.toDouble();
                                                QString text=value.toString();
                                                int val=text.toInt();
                                                ui->ty_y->setChecked(val);

                                            }
                                            break;
                                        default:
                                            break;
                                        }
}
                                    }
                                }
                            }
                        }
                    }else
                    {
                        qDebug() <<"result错误";
                    }
                }

            }
        }
    }
}

